<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>会员列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/resource/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/resource/css/mylay.css">
</head>
<body>
    <div style="padding: 20px; background-color: #F2F2F2;">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card" style="padding: 10px;">
                    <form class="layui-form search">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">账号</label>
                                <div class="layui-input-inline">
                                    <input class="layui-input" name="username" autocomplete="off">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">UID</label>
                                <div class="layui-input-inline">
                                    <input class="layui-input" name="uid" autocomplete="off">
                                </div>
                            </div>
                            <!-- <div class="layui-inline">
                                <label class="layui-form-label">余额</label>
                                <div class="layui-input-inline" style="width: 100px;">
                                    <input type="text" name="balance1" placeholder="￥" class="layui-input">
                                </div>
                                <div class="layui-form-mid">-</div>
                                <div class="layui-input-inline" style="width: 100px;">
                                    <input type="text" name="balance2" placeholder="￥" class="layui-input">
                                </div>
                            </div> -->
                            <div class="layui-inline">
                                    <label class="layui-form-label">账号状态</label>
                                    <div class="layui-input-inline">
                                        <select name="state" lay-search="">
                                            {foreach $Think.config.custom.userState as $key=>$value}
                                            <option value="{$key}">{$value}</option>
                                            {/foreach}
                                        </select>
                                    </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">注册时间</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="datetime_range" class="layui-input" readonly>
                                </div>
                            </div>
                            <div class="layui-inline" style="text-align: center;">
                                <button type="button" class="layui-btn" data-type="search">搜索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="layui-col-md12">
                <div class="layui-card">
                    <table class="layui-hide" id="userList" lay-filter="userList"></table>
                </div>
            </div>
        </div>
    </div>
    <!-- 头部左侧工具栏 -->
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container layui-btn-group">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary useradd_btn" lay-event="add">
                新增会员
            </button>
            <!-- <button type="button" class="layui-btn layui-btn-sm layui-btn-primary" lay-event="creditAssess">信用评估</button> -->
        </div>
    </script>
    <!-- 表单元素 -->
    <script type="text/html" id="lockAccount">
        <input type="checkbox" name="lockAccount" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="userAction" data-suburi="lockAccount" {{ d.state == 2 ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="danger">
        <input type="checkbox" name="danger" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="userAction" data-suburi="risk" {{ d.danger == 1 ? 'checked' : '' }}>
    </script>
    <script type="text/html" id="action">
    	<div class="layui-btn-group">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="freezeTrade">冻结交易</button>
    		<button type="button" class="layui-btn layui-btn-xs" lay-event="edit">
    			详情
    		</button>
            <button type="button" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>
            <button type="button" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="mail">发送邮件</button>
	    </div>
    </script>


<script src="/resource/layuiadmin/layui/layui.js"></script>
<script src="/resource/js/manage/init_date.js"></script>
<!--<script src="/resource/js/manage/user.js"></script>-->
<script>
    layui.use(['table','form'], function(){
        var $ = layui.$
            ,form = layui.form
        ,table = layui.table;

        //方法级渲染
        table.render({
            elem: '#userList'
            ,title: '用户列表'
            ,url: '/manage/user/userList'
            ,method: 'post'
            ,cols: [[
                // {checkbox: true, fixed: true, totalRowText: '合计'}
                {field: 'id', title: 'UID'}
                ,{field: 'nickname', title: '用户名'}
                ,{field: 'username', title: '账号'}
                ,{field: 'userTypeStr', title: '账号类型'}
				// ,{field: 'realname', title: '实名', sort: true,}
                // ,{field: 'grade_name', title: '等级'}
                ,{field: 'balance', title: 'OTC余额(USDT)'}
                ,{field: 'roy_number', title: '版税碎片'}
                ,{field: 'fee_number', title: '手续费碎片'}
                ,{field: 'pledge_number', title: '质押碎片'}
                ,{field: 'total_pledge_number', title: '总质押碎片'}
                ,{field: 'goods_total_number', title: '总持有碎片', templet: function(d){
                        if (d.goods_total_number>0){
                            return "<div style='color:blue' lay-event='get_goods_list'>"+d.goods_total_number+"</div>";
                        }
                        return d.goods_total_number;
                }}
                ,{field: 'today_order_count', title: '每日参与预售次数'}
                ,{field: 'today_sell_count', title: '每日卖出单量'}
                ,{field: 'today_buy_count', title: '每日买入单量'}
                ,{field: 'reg_time', title: '注册时间'}
                ,{field: 'last_ip', title: '注册IP'}
                ,{field: 'stateStr', title: '状态'}
                ,{field: 'last_ip', title: '使用IP'}
                ,{field: 'recommend', title: '上级'}
                ,{title: '冻结', templet: '#lockAccount', unresize: true}
                // ,{field: 'danger', title: '风险账号', sort: true, templet: '#danger', unresize: true}
                ,{title: '操作', width: '20%', toolbar: '#action'}
            ]]
            ,cellMinWidth: 100
            ,toolbar: '#toolbarDemo'
            ,defaultToolbar: []
            ,totalRow: true
            ,page: {
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            }
            ,skin: 'row' //行边框风格
            ,even: true //开启隔行背景
        });

        // 风险账号开关
        form.on('switch(userAction)', function(obj){
            var val = obj.elem.checked ? 1 : 2;
            var uri = obj.elem.dataset.suburi;
            $.ajax({
                url: "/manage/user/"+uri,
                data: {
                    uid: this.value,
                    value: val
                },
                type: "POST",
                dataType: "json",
                timeout: 15000,
                beforeSend: function(){
                    layer.load(1);
                },
                success: function(msg){
                    var alertStr = (msg == 1) ? '操作成功' : msg;
                    layer.msg(alertStr);
                },
                complete: function(){
                    layer.closeAll("loading");
                }
            });
        });

        // 监听工具条
        table.on('tool(userList)', function(obj){
            var title = '删除';
            if (obj.event=='freezeTrade'){
                title = '冻结交易';
            }
            switch (obj.event) {
                case 'capital':
                    layer.open({
                        type: 2,
                        area: ['45%', '55%'],
                        title: '用户资金 - ID：'+obj.data.id+' 账号：'+obj.data.username,
                        content: '/manage/user/capital?id='+obj.data.id
                    });
                    break;

                case 'edit':
                    layer.open({
                        type: 2,
                        area: ['100%', '90%'],
                        title: '用户编辑 - ID：'+obj.data.id+' 账号：'+obj.data.username,
                        content: '/manage/user/edit?id='+obj.data.id
                    });
                    break;

                case 'get_goods_list':
                    layer.open({
                        type: 2,
                        area: ['75%', '95%'],
                        title: obj.data.username+'资金碎片列表',
                        content: '/manage/goods/get_list?uid='+obj.data.id
                    });
                    break;

                case 'mail':
                    layer.open({
                        type: 2,
                        area: ['65%', '55%'],
                        title: obj.data.username+'发送邮件',
                        content: '/manage/user/mail?id='+obj.data.id
                    });
                    break;

                default:
                    layer.confirm('确定'+title+obj.data.username+'？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        $.ajax({
                            url: "/manage/user/action",
                            data: {
                                table: 'Users',
                                action:obj.event,
                                id: obj.data.id,
                                name: obj.data.username,
                            },
                            type: "POST",
                            dataType: "json",
                            timeout: 15000,
                            beforeSend: function(){
                                layer.load(1);
                            },
                            success: function(msg){
                                var alertStr = (msg == 1) ? '操作成功' : msg;
                                layer.msg(alertStr, {time: 2000}, function(){
                                    if(msg==1){
                                        layer.closeAll();
                                        layui.table.reload('userList');
                                    }
                                });
                            },
                            complete: function(){
                                layer.closeAll("loading");
                            }
                        });
                    }, function(){
                        layer.msg('取消操作', {
                            time: 2000, //2s后自动关闭
                        });
                    });
                    break;
            }
        });

        table.on('toolbar(userList)', function(obj){
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2,
                        area: ['500px','400px'],
                        title: '添加账户',
                        content: '/manage/user/add'
                    });
                    break;
                case 'creditAssess':
                    layer.confirm('确定评估上周信用？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        $.ajax({
                            url: "/manage/user/creditAssess",
                            data: {

                            },
                            type: "POST",
                            dataType: "json",
                            timeout: 15000,
                            beforeSend: function(){
                                layer.load(1);
                            },
                            success: function(msg){
                                var alertStr = (msg == 1) ? '操作完成' : msg;
                                layer.msg(alertStr, {time: 2000}, function(){
                                    if(msg==1){
                                        layer.closeAll();
                                        layui.table.reload('userList');
                                    }
                                });
                            },
                            complete: function(){
                                layer.closeAll("loading");
                            }
                        });
                    }, function(){
                        layer.msg('取消操作', {
                            time: 2000, //2s后自动关闭
                        });
                    });
                    break;
            };
        });

        //监听排序事件
        table.on('sort(userList)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            //尽管我们的 table 自带排序功能，但并没有请求服务端。
            //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
            table.reload('userList', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    sortField: obj.field //排序字段
                    ,sortType: obj.type //排序方式
                }
            });
        });

        active = {
            search: function(){
                //执行重载
                table.reload('userList', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        username: $("input[name='username']").val()
                        ,uid: $("input[name='uid']").val()
                        ,balance1: $("input[name='balance1']").val()
                        ,balance2: $("input[name='balance2']").val()
                        ,state: $("select[name='state'] option:selected").val()
                        ,is_automatic: $("select[name='is_automatic'] option:selected").val()
						,idcode:$("input[name='idcode']").val()

                    }
                }, 'data');
            }
        };

        $('.search .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
